package com.ycsj.goldmedalnewconcept.parent.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.SrfManager;
import com.ycsj.common.utils.GsonUtil;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.ycsj.goldmedalnewconcept.parent.adapter.RedPackageRvAdapter;
import com.ycsj.goldmedalnewconcept.parent.bean.RedPackageInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 我的红包
 * Created by Administrator on 2018/6/21 0021.
 */

public class ParentsRedPackageListActivity extends BaseActivity implements SrfManager.OnPullRefreshListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srf)
    SmartRefreshLayout srf;
    @BindView(R.id.rl_loading_view)
    RelativeLayout rlLoadingView;
    @BindView(R.id.rl_content)
    RelativeLayout rlContent;

    //数据集合
    private List<RedPackageInfo.RedPacketBean> oldDatas;

    //适配器
    private RedPackageRvAdapter adapter;

    //空视图
    private View emptyView;
    private ImageView iv_icon;

    @Override
    protected int getContentLayout() {
        return R.layout.parent_activity_red_package;
    }

    @Override
    protected void init() {

        initStateBar();

        //初始化视图
        initTitle();

        //初始化下拉刷新控件
        initSrf();

        //设置数据
        initRv();

        //请求数据
        http(BqaManager.NORML);
    }

    /**
     * 初始化视图
     */
    public void initTitle() {
        tvTitle.setText("我的红包");
    }


    /**
     * 初始化下拉刷新
     */
    private void initSrf() {
        SrfManager.setSmartByPullListener(srf, this);
    }

    /**
     * 请求服务器数据的方法
     *
     * @param method
     */
    private void http(final int method) {

        Map<String, String> map = new HashMap<>();
        map.put("type", "8");
        map.put("c", AccountUtils.getAccount(this));
        map.put("role", AccountUtils.getRole(this));
        map.put("platform", "8");

        HttpUtils.post(HttpAddress.PARENTREDLIST, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoadingView, srf, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoadingView != null && rlLoadingView.getVisibility() == View.VISIBLE) {
                    rlLoadingView.setVisibility(View.GONE);
                }

                if (rlContent != null && rlContent.getVisibility() != View.VISIBLE) {
                    rlContent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    RedPackageInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, RedPackageInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info && "1".equals(info.getState())) {
                        //返回成功
                        List<RedPackageInfo.RedPacketBean> newDatas = info.getRedPacket();
                        Collections.reverse(newDatas);//倒序处理
                        if (newDatas != null) {
                            if (method == BqaManager.NORML) {
                                oldDatas.clear();
                                oldDatas.addAll(newDatas);
                                BqaManager.setRv(emptyView, ParentsRedPackageListActivity.this, adapter, rv);
                            } else {
                                //更新数据
                                BqaManager.updateRvBySmf(method, srf, rv, oldDatas, newDatas, adapter);
                                adapter.setEnableLoadMore(false);
                            }
                        }
                    } else if ("-1".equals(info.getState())) {
                        BqaManager.setRv(emptyView, ParentsRedPackageListActivity.this, adapter, rv);

                        Log.i("test", Constants.ERRORSTATE + info.getState());
                    } else {
                        UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                        Log.i("test", Constants.ERRORSTATE + info.getState());
                    }
                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 设置列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        emptyView = View.inflate(this, R.layout.base_rv_common_empty_view, null);
        TextView tvMsg = emptyView.findViewById(R.id.tv_content);
        tvMsg.setText("暂无红包");
        iv_icon = emptyView.findViewById(R.id.iv_icon);
        iv_icon.setImageResource(R.drawable.base_empyt_redbag);
        adapter = new RedPackageRvAdapter(this, R.layout.parent_rv_red_package, oldDatas);
    }


    /**
     * 下拉刷新
     *
     * @param refreshlayout
     */
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        http(BqaManager.PULLREFRESH);
    }

    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back})
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }

}
